<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
use tcpdf\tcpdf;
class Cashout extends Base{
    public function cashout_list(){
        $keyword=input('get.keyword');
        $style=input('get.style');
        $state=input('get.state');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $map = [
            ['a.time', '>', $fromtime],
            ['a.time', '<', $totime]
        ];
        if($state==0){
            array_push($map,['a.state','<',4]);
        }else{
            array_push($map,['a.state','=',$state]);
        }
        if($style==0){
            array_push($map,['a.style','<',3]);
        }else{
            array_push($map,['a.style','=',$style]);
        }
        $list=Db::name('cashout')->alias('a')->join('users b', 'a.user_id = b.id','LEFT')->field('a.*,b.phone')->where($map)->where('a.orderno|a.card_no|a.name|b.phone','like',"%{$keyword}%")->order('a.id desc')->paginate(10);
        $count=Db::name('cashout')->alias('a')->join('users b', 'a.user_id = b.id','LEFT')->field('a.*,b.phone')->where($map)->where('a.orderno|a.card_no|a.name|b.phone','like',"%{$keyword}%")->count();
        $this->assign('keyword',input('get.keyword'));
        $this->assign('style',$style);
        $this->assign('state',$state);
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('list',$list);
        $this->assign('count',$count);
        return $this->fetch();
    }
    public function cashout_detail(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('cashout')->alias('a')->join('users b', 'a.user_id = b.id','LEFT')->field('a.*,b.phone')->where('a.id',$param['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['state']=$param['state'];
            $data['uptime']=time();
            Db::startTrans();
            try {
                $update=Db::name('cashout')->where('id',$param['id'])->update($data);
                if($update){
                    $cashout=Db::name('cashout')->where('id',$param['id'])->find();
                    if($param['state']==2){
                        $res=Db::name('money_bill')->insert(['user_id'=>$cashout['user_id'],'orders_id'=>$param['id'],'orderno'=>$cashout['orderno'],'type'=>3,'money'=>$cashout['cash'],'time'=>time(),'uptime'=>time()]);
                    }elseif($param['state']==3){
                        $res=Db::name('users')->where('id', $cashout['user_id'])->setInc('money',$cashout['cash']);
                    }
                }
                // 提交事务
                Db::commit();
                if($res){
                    return json(['code' => 200, 'msg' => '成功！']);
                }else{
                    return json(['code' => 500, 'msg' => '事务提交失败！']);
                }
            } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 500, 'msg' => '事务回滚失败！']);
        }
        }
    }
    
}
